package com.cskaoyan.service.wx;

import com.cskaoyan.bean.category.Category;
import com.cskaoyan.bean.po.*;
import com.cskaoyan.bean.wx.cart.UserAvailableCoupon;
import com.cskaoyan.mapper.MarketAddressMapper;
import com.cskaoyan.mapper.MarketCartMapper;
import com.cskaoyan.mapper.MarketCouponUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.cert.CRLReason;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class WxCartServiceImpl implements WxCartService {
    @Autowired
    MarketCartMapper marketCartMapper;

    @Override
    public List<MarketCart> queryCartListByUserId(Integer userId) {
        MarketCartExample marketCartExample = new MarketCartExample();
        MarketCartExample.Criteria criteria = marketCartExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andDeletedEqualTo(false);
        List<MarketCart> marketCarts = marketCartMapper.selectByExample(marketCartExample);
        return marketCarts;
    }

    @Override
    public Integer updateChecked(Integer userId, Integer productId, Integer isChecked) {
        boolean checked = false;
        if (isChecked!=0){
            checked = true;
        }
        MarketCart marketCart = new MarketCart();
        marketCart.setChecked(checked);
        marketCart.setUpdateTime(new Date());
        MarketCartExample marketCartExample = new MarketCartExample();
        MarketCartExample.Criteria criteria = marketCartExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andDeletedEqualTo(false);
        criteria.andProductIdEqualTo(productId);
        int i = marketCartMapper.updateByExampleSelective(marketCart, marketCartExample);
        return i;
    }

    @Override
    public Boolean isCartExist(Integer userId, Integer goodsId, Integer productId) {
        MarketCartExample marketCartExample = new MarketCartExample();
        MarketCartExample.Criteria criteria = marketCartExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andGoodsIdEqualTo(goodsId);
        criteria.andProductIdEqualTo(productId);
        criteria.andDeletedEqualTo(false);
        List<MarketCart> marketCarts = marketCartMapper.selectByExample(marketCartExample);
        return marketCarts.size() != 0;
    }


    @Override
    public Integer updateCart(Integer userId, Integer goodsId, Integer productId,Integer number) {
        MarketCartExample marketCartExample = new MarketCartExample();
        MarketCartExample.Criteria criteria = marketCartExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andGoodsIdEqualTo(goodsId);
        criteria.andProductIdEqualTo(productId);
        criteria.andDeletedEqualTo(false);
        List<MarketCart> marketCarts = marketCartMapper.selectByExample(marketCartExample);
        MarketCart marketCart = marketCarts.get(0);
        number+=marketCart.getNumber();
        marketCart.setNumber(number);
        marketCart.setUpdateTime(new Date());
        int update = marketCartMapper.updateByPrimaryKey(marketCart);
        return update;
    }

    @Override
    public Integer addCart(Integer userId, Integer goodsId, Integer productId, Integer number) {
        MarketCart marketCart = marketCartMapper.selectCartByGoodIdAndProductId(goodsId, productId);
        marketCart.setUpdateTime(new Date());
        marketCart.setUserId(userId);
        marketCart.setAddTime(new Date());
        marketCart.setChecked(false);
        marketCart.setDeleted(false);
        marketCart.setNumber(number);
        int insert = marketCartMapper.insertSelective(marketCart);
        return insert;
    }

    @Autowired
    MarketAddressMapper marketAddressMapper;

    @Override
    public MarketAddress queryDefaultAddressByUserId(Integer userId) {
        //查询默认地址
        MarketAddressExample marketAddressExample = new MarketAddressExample();
        MarketAddressExample.Criteria criteria = marketAddressExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andIsDefaultEqualTo(true);
        criteria.andDeletedEqualTo(false);
        List<MarketAddress> marketAddresses = marketAddressMapper.selectByExample(marketAddressExample);
        if (marketAddresses.size() != 0) {
            MarketAddress marketAddress = marketAddresses.get(0);
            return marketAddress;
        }
        return null;
    }

    @Override
    public List<MarketCart> queryCartListByUserIdAndChecked(Integer userId, boolean checked) {
        MarketCartExample marketCartExample = new MarketCartExample();
        MarketCartExample.Criteria criteria = marketCartExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        criteria.andUserIdEqualTo(userId);
        criteria.andCheckedEqualTo(checked);
        List<MarketCart> marketCarts = marketCartMapper.selectByExample(marketCartExample);
        return marketCarts;
    }


    @Override
    public List<UserAvailableCoupon> queryUserCouponByUserId(Integer userId) {
        //联表查询返回UserAvailableCoupon list
        List<UserAvailableCoupon> userAvailableCoupons = marketCartMapper.selectUserAvailableCouponByUserId(userId);
        return userAvailableCoupons;
    }

    @Override
    public Integer updateCartById(Integer cartId, Integer number) {
        MarketCart marketCart = new MarketCart();
        marketCart.setId(cartId);
        marketCart.setNumber(number);
        int update = marketCartMapper.updateByPrimaryKeySelective(marketCart);
        return update;
    }

    @Override
    public Integer deleteCartByUserIdAndProductIds(Integer userId, ArrayList<Integer> productIds) {
        //要更新的对象
        MarketCart marketCart = new MarketCart();
        marketCart.setDeleted(true);
        marketCart.setUpdateTime(new Date());
        //指定更新的条件
        MarketCartExample marketCartExample = new MarketCartExample();
        MarketCartExample.Criteria criteria = marketCartExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andProductIdIn(productIds);
        //更新
        int update = marketCartMapper.updateByExampleSelective(marketCart, marketCartExample);
        return update;
    }

    @Override
    public MarketCart queryCartByGoodsIdAndProductId(Integer goodsId, Integer productId) {
        MarketCart marketCart = marketCartMapper.selectCartByGoodIdAndProductId(goodsId, productId);
        return marketCart;
    }

    /**
     * 这里返回的是cart的id
     * @param marketCart
     * @return
     */
    @Override
    public Integer addCartByMarketCart(MarketCart marketCart) {
        int insert = marketCartMapper.insertSelective(marketCart);
        return marketCart.getId();
    }

    @Override
    public MarketAddress queryAddressByAddressId(Integer addressId) {
        MarketAddress marketAddress = marketAddressMapper.selectByPrimaryKey(addressId);
        return marketAddress;
    }
}
